package com.qf.openapi.web.master.Interceptor;

import com.qf.openapi.web.master.entity.User;
import com.qf.openapi.web.master.utils.SessionKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j//引入日志对象
public class LoginStatusInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("请求进入拦截器，请求的url={}", request.getRequestURI());
        HttpSession session = request.getSession();
        User token = (User) session.getAttribute(SessionKey.user_Login_Session_Key);
        System.out.println(token);
        if (token == null) {
            log.debug("请求被拦截了，重定向到登录页");
            //让浏览器重定向到登录页
            response.sendRedirect("/login.html");
            return false;
        }
        log.debug("请求被放行了");
        return true;
    }
}
